﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using WinRTFramework.FrameworkConfiguration;
using WinRTFramework.Interfaces.Contracts.Settings;

namespace WinRTFramework.FrameworkCore.Services
{
    public class SettingService : ISettingService
    {
        private readonly IViewViewModelMapper viewProvider;
        private readonly IIoCAdapter iocAdapter;

        public SettingService(IViewViewModelMapper viewProvider, IIoCAdapter iocAdapter)
        {
            this.viewProvider = viewProvider;
            this.iocAdapter = iocAdapter;
        }

        public void ShowIndependent<VMType>(object parameter = null)
        {
            var viewType = this.viewProvider.GetViewTypeForViewModelType(typeof(VMType), parameter);
            if (viewType != null)
            {
                var view = this.iocAdapter.GetInstanceOfType(viewType);
                var viewModel = this.iocAdapter.GetInstanceOfType(typeof(VMType));
                if (view != null && view is SettingsFlyout && viewModel != null)
                {
                    (view as SettingsFlyout).DataContext = viewModel;
                    (view as SettingsFlyout).ShowIndependent();
                }
            }
        }
    }
}
